package com.wz.callback;

import java.util.Arrays;
import java.util.List;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.example.wzsdk.login.Landing;
import com.example.wzsdk.login.LoginState;
import com.example.wzsdk.scan.Search.SearchFinsh;
import com.wz.bean.ServerBean;
import com.wz.timetask.TimeJob;

@javax.servlet.annotation.WebListener
public class WebListener implements ServletContextListener, LoginState, SearchFinsh {

	public static String[][] ip_list = {
			{ "192.168.1.102", "1637530539" } 
			,
		{ "192.168.1.106", "1693760097" }
			,
		{ "192.168.1.115", "351628381" }
			,
		{ "192.168.1.113", "1652666517" }
			,
			{ "192.168.1.107", "1758640737" }
			};
	public static Landing[] landings = { 
		new Landing("sdaewyufajsfg")
		,new Landing("qweothjdn") 
			,new Landing("xaffefaf") 
		,new Landing("iudfghvcnb") 
		,new Landing("acfnhdhds") 
			};

	public static boolean[] state = { 
		false 
		,false
		,false
		,false
		,false
		};

	public static ServerBean[] listbens = { 
		new ServerBean()
		,new ServerBean()
		,new ServerBean() 
		,new ServerBean() 
		,new ServerBean() 
	};

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("结束");

	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("启动");
		Timer timer = new Timer();
		TimeJob timeJob = new TimeJob(ip_list, state, this, landings, listbens);
		timer.schedule(timeJob, 100, 60 * 1000);
	}

	@Override
	public void LoginStatus(boolean state, String sn) {
		if (state == false) {
			for (int i = 0; i < ip_list.length; i++) {
				if (ip_list[i][1].equals(sn)) {
					this.state[i] = false;
					System.out.println("信息：WebListener--------------主机（"
							+ ip_list[i][1] + "）" + "   ip（" + ip_list[i][0]
							+ "）已退出--------------");
//					landings[i].exitLogin();
				}
			}

		} else {
			for (int i = 0; i < ip_list.length; i++) {
				if (ip_list[i][1].equals(sn)) {
					System.out.println(i);
					this.state[i] = true;
					System.out.println("信息：WebListener--------------主机（"
							+ ip_list[i][1] + "）" + "   ip（" + ip_list[i][0]
							+ "）成功登陆主机--------------");
					listbens[i].setState(state + "");
					listbens[i].setSn(sn);
				}
			}
			can(sn);
		}
	}

	@Override
	public void AttentionStarting(String name, byte num, String index,String id, String sn) {
		System.out.println(name + "  " + num + "  " + index + "  " + sn);
		for (int i = 0; i < ip_list.length; i++) {
			if (sn.equals(ip_list[i][1])) {
				listbens[i].setNum(name, num+"",index, sn);
			}
		}
	}

	@Override
	public void AttentionProcess(String name, byte num, String text, String index,String id,String sn) {
		System.out.println(name + "  " + num + "  " + text + "  " + sn);
		for (int i = 0; i < ip_list.length; i++) {
			if (sn.equals(ip_list[i][1])) {
				listbens[i].setNum(name, num+"",index, sn);
			}
		}
	} 

	@Override
	public void SetVarState(String name, byte num, String sn) {
		System.out.println(name + "  " + num + "  " + "  " + sn);
		for (int i = 0; i < ip_list.length; i++) {
			if (sn.equals(ip_list[i][1])) {
				listbens[i].setNum(name, num+"","1", sn);
			}
		}
	}

	public static List<String[]> data;

	public void can(String sn) {
		for (int i = 0; i < ip_list.length; i++) {
			if (sn.equals(ip_list[i][1])) {
				data = landings[i].getDateList();
				for (int j = 0; j < data.size(); j++) {
					System.out.println("信息：WebListener--------------主机（"
							+ ip_list[i][1] + "）"
							+ Arrays.toString(data.get(j)) + "-------------");
				}
			}
		}
	}

	public int selectorI(String sn) {
		int j = -1;
		for (int i = 0; i < WebListener.listbens.length; i++) {
			if (WebListener.listbens[i].getSn().equals(sn)) {
				j = i;
			}
		}
		return j;
	}


	@Override
	public void SetKeyState(String name, String state, String sn) {
		System.out.println(name + "  " + state + "  " + "  " + sn);
		
		for (int i = 0; i < ip_list.length; i++) {
			if (sn.equals(ip_list[i][1])) {
				listbens[i].setState(state);
			}
		}
		
	}

	@Override
	public void SearchFinsh(List<String[]> list) {
		for (int i = 0; i < list.size(); i++) {
			System.out.println(Arrays.toString(list.get(i)));
		}
	}

}
